#include "service.h"
#include "../param.h"
#include "siteService/nlohmann/json.hpp"
#include "siteService/service_site_manager.h"
#include "log/Logging.h"
#include "qlibc/QData.h"
#include "common/httpUtil.h"
#include "../sourceManage/siteRequestUtil.h"
#include <vector>
#include <string>

using namespace std;
using namespace servicesite;
using namespace std::placeholders;

void subscribe_radar_message()
{   
    while(true){
        bool subscribeFlag = false;
        qlibc::QData response;
        if(siteRequestUtil::access_service_master_get_app_site_list("127.0.0.1", response)){
            LOG_BLUE << "get_app_site_list response: " << response.toJsonString();
            qlibc::QData app_site_list = response.getData("response").getData("app_site_list");
            for(Json::ArrayIndex i = 0; i < app_site_list.size(); ++i){
                qlibc::QData ithData = app_site_list.getArrayElement(i);
                string site_id = ithData.getString("site_id");
                string run_state = ithData.getString("run_state");
                if(site_id == "radar" && run_state == "RUN_OK"){    //雷达站点
                    string ip = ithData.getString("ip");
                    std::vector<string> radarMessageIdList;
                    radarMessageIdList.push_back(Radar_Msg_MessageID);
                    int code = ServiceSiteManager::getInstance()->subscribeMessage(ip, 9003, "radar", radarMessageIdList);
                    if (code == ServiceSiteManager::RET_CODE_OK) {
                        LOG_PURPLE << "subscribeMessage radarPoints ok....";
                        subscribeFlag = true;
                        break;
                    }
                }
            }
            if(subscribeFlag){
                return;
            }
        }
        LOG_RED << "subscribeMessage radarPoints error....";
        std::this_thread::sleep_for(std::chrono::seconds(10));
    }
}


void subscribe_mqttservice_message()
{
    while(true){
        bool subscribeFlag = false;
        //从主从站点获取站点列表
        qlibc::QData response;
        if(siteRequestUtil::access_service_master_get_app_site_list("127.0.0.1", response)){
            LOG_BLUE << "get_app_site_list response: " << response.toJsonString();
            qlibc::QData app_site_list = response.getData("response").getData("app_site_list");
            for(Json::ArrayIndex i = 0; i < app_site_list.size(); ++i){
                qlibc::QData ithData = app_site_list.getArrayElement(i);
                string site_id = ithData.getString("site_id");
                string run_state = ithData.getString("run_state");
                if(site_id == "mqtt_service" && run_state == "RUN_OK"){
                    string ip = ithData.getString("ip");
                    std::vector<string> mqttMessageIdList;
                    mqttMessageIdList.push_back(ReportLog_MessageID);   
                    int code = ServiceSiteManager::getInstance()->subscribeMessage(ip, 9020, "mqtt_service", mqttMessageIdList);
                    if (code == ServiceSiteManager::RET_CODE_OK) {
                        LOG_PURPLE << "subscribeMessage reportLog ok....";
                        subscribeFlag = true;
                        break;
                    }
                }
            }
            if(subscribeFlag){
                return;
            }
        }
        LOG_RED << "subscribeMessage report log failed ....";
        std::this_thread::sleep_for(std::chrono::seconds(10));
    }
}


void subscribe_master_message()
{
    bool subscribeMasterFlag=false, subscribeConfigFlag=false;
    while(true){
        if(!subscribeMasterFlag){
            std::vector<string> messageIdList;
            messageIdList.push_back(Site_List_Update_MessageID);
            messageIdList.push_back(AllUpdate_MessageID);
            //获取雷达站点ip
            int code = ServiceSiteManager::getInstance()->subscribeMessage("127.0.0.1", 9012, "master_coordinate", messageIdList);
            if (code == ServiceSiteManager::RET_CODE_OK) {
                subscribeMasterFlag = true;
                LOG_PURPLE << "subscribeMessage app_site_list_update ok....";
            }
        }
        
        if(!subscribeConfigFlag){
            std::vector<string> messageIdList;
            messageIdList.push_back(WhiteListChange_MessageID);
            //获取雷达站点ip
            int code = ServiceSiteManager::getInstance()->subscribeMessage("127.0.0.1", 9006, "config", messageIdList);
            if (code == ServiceSiteManager::RET_CODE_OK) {
                subscribeConfigFlag = true;
                LOG_PURPLE << "subscribeMessage whiteList change ok....";
            }
        }

        if(!subscribeMasterFlag || !subscribeConfigFlag){
            std::this_thread::sleep_for(std::chrono::seconds(10));
            LOG_RED << "subscribeMessage fixed local site failed....., start to subscribe in 5 seconds";
        }else{
            break;
        }
    }
}

void subscribe_query_message()
{
    while (true){
        bool subscribeFlag = true;
        qlibc::QData response;
        if(siteRequestUtil::access_service_master_get_all("127.0.0.1", response)){
            qlibc::QData node_list = response.getData("response").getData("node_list");
            for(Json::ArrayIndex i = 0; i < node_list.size(); ++i){
                qlibc::QData ithData = node_list.getArrayElement(i);
                string ip = ithData.getString("ip");
                std::vector<string> mqttMessageIdList;
                mqttMessageIdList.push_back(Site_OnOffLine_MessageID);   
                int code = ServiceSiteManager::getInstance()->subscribeMessage(ip, 9000, "site_query", mqttMessageIdList);
                if (code != ServiceSiteManager::RET_CODE_OK) {
                    subscribeFlag = false;
                }
            }
            if(subscribeFlag){
                LOG_PURPLE << "subscribeMessage site_onoffline ok....";
                return;
            }
        }
        std::this_thread::sleep_for(std::chrono::seconds(10));
        LOG_RED << "subscribeMessage site_onoffline failed ....";
    }
}
